//
//  ViewController.m
//  pintu
//
//  Created by Fxxx on 2017/11/29.
//  Copyright © 2017年 lu. All rights reserved.
//

#import "ViewController.h"
#import "PTHandler.h"

@interface ViewController () <PTHandlerDelegate ,UINavigationControllerDelegate ,UIImagePickerControllerDelegate>

@property (strong ,nonatomic) PTHandler *handler;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionview;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UIImageView *backgroundImageView;

@property (strong ,nonatomic) UIImage *image;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    _image = [UIImage imageNamed:@"IMG_0370.JPG"];
    [_collectionview registerClass:ImageCell.class forCellWithReuseIdentifier:@"ImageCell"];
    _handler = [[PTHandler alloc] initWithImage:_image level:PT3X3 delegate:self];
    
    _imageView.image = _handler.image;
    _backgroundImageView.image = _handler.image;
    [_imageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(chooseImage:)]];
}

#pragma mark - PTHandler delegate
- (void)handlerImages:(NSArray <UIImage *>*)images didChangeIndexes:(NSArray <NSNumber *>*)indexes; {
    [_collectionview reloadData];
}

#pragma mark - image picker delegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(nullable NSDictionary<NSString *,id> *)editingInfo NS_DEPRECATED_IOS(2_0, 3_0); {
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info; {
    [picker dismissViewControllerAnimated:YES completion:nil];
    UIImage *image = info[UIImagePickerControllerEditedImage];
    if (!image) {
        image = info[UIImagePickerControllerOriginalImage];
    }
    _image = image;
    PTLevel level = PT3X3;
    if (_handler) {
        level = _handler.level;
    }
    _handler = [[PTHandler alloc] initWithImage:_image level:level delegate:self];
    _imageView.image = _handler.image;
    _backgroundImageView.image = _handler.image;
    [_collectionview reloadData];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker; {
    [picker dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - collection view
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section; {
    return _handler.indexes.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath; {
    ImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ImageCell" forIndexPath:indexPath];
    UIImage *image = _handler.images[_handler.indexes[indexPath.row].integerValue];
    if ([image isKindOfClass:[NSNull class]]) {
        image = nil;
    }
    cell.imageview.image = image;
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath; {
    CGFloat contentwidth = collectionView.frame.size.width;
    UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)collectionViewLayout;
    contentwidth = contentwidth - layout.sectionInset.left - layout.sectionInset.right;
    contentwidth = contentwidth - (_handler.level - 1) * layout.minimumInteritemSpacing;
    CGFloat itemwidth = contentwidth / (float)_handler.level;
    if (itemwidth < 0) itemwidth = 0;
    return CGSizeMake(itemwidth, itemwidth);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [_handler update:indexPath.row];
}

#pragma mark - actions

// 切换难度
- (IBAction)clickActions:(id)sender {
    PTLevel level = [sender tag] + 2;
    _handler = [[PTHandler alloc] initWithImage:_image level:level delegate:self];
    _imageView.image = _handler.image;
    _backgroundImageView.image = _handler.image;
    [_collectionview reloadData];
}

// 选择本地图片
- (void)chooseImage:(UITapGestureRecognizer *)tap {
    UIImagePickerController *ipvc = [[UIImagePickerController alloc] init];
    ipvc.allowsEditing = YES;
    ipvc.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    ipvc.delegate = self;
    [self presentViewController:ipvc animated:YES completion:nil];
}

// 开始(打乱顺序)
- (IBAction)start:(id)sender {
    [_handler upset];
}

@end

@implementation ImageCell

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        _imageview = [[UIImageView alloc] initWithImage:nil];
        _imageview.backgroundColor = [UIColor lightGrayColor];
        [self addSubview:_imageview];
    } return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    _imageview.frame = self.bounds;
}

@end


@implementation BlurImageView

- (void)awakeFromNib {
    [super awakeFromNib];
    UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    _visualEffectView = [[UIVisualEffectView alloc] initWithEffect:effect];
    [self addSubview:_visualEffectView];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    _visualEffectView.frame = self.bounds;
}

@end
